#include "Sort.h"

// 打印数组
void PrintArray(int* a, int n)
{
    assert(a);

    for (int i = 0; i < n; i ++ )
    {
        printf("%d ", a[i]);
    }
    printf("\n");
}

void swap(int* a, int* b)
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

// 插入排序
void InsertSort(int* a, int n)
{
    assert(a);

    for (int i = 1; i < n; i ++ )
    {
        int end = i - 1;
        int tmp = a[i];

        while (end >= 0)
        {
            if (a[end] > tmp)
            {
                a[end + 1] = a[end];
                end -- ;
            }
            else 
            {
                break;
            }
        }
        a[end + 1] = tmp;
    }
}

// 希尔排序
void SherSort(int* a, int n)
{
    assert(a);

    int gap = n;

    while (gap > 1)
    {
        gap /= 2;

        for (int i = 0; i < n - gap; i ++ )
        {
            int end = i;
            int tmp = a[i + gap];

            while (end >= 0)
            {
                if (a[end] > tmp)
                {
                    a[end + gap] = a[end];
                    end -= gap;
                }
                else 
                {
                    break;
                }
            }

            a[end + gap] = tmp;
        }
    }
}

// 选择排序
void SelectSort(int* a, int n)
{
    assert(a);

    int l = 0;
    int r = n - 1;
    
    while (l < r)
    {
        int maxi = l, mini = l;
        for (int i = l; i <= r; i ++ )
        {
            if (a[i] < a[mini]) mini = i;
            if (a[i] > a[maxi]) maxi = i;
        }
        swap(&a[l], &a[mini]);
        if (maxi == l)
        {
            maxi = mini;
        }
        swap(&a[r], &a[maxi]);

        l ++ ;
        r -- ;
    }
}

void adjustdown(int* a, int n, int parent)
{
    int child = parent * 2 + 1;
    while (child < n)
    {
        if (child + 1 < n && a[child + 1] > a[child]) child ++ ;
        if (a[child] > a[parent])
        {
            swap(&a[child], &a[parent]);
            parent = child;
            child = parent * 2 + 1;
        }
        else 
        {
            break;
        }
    }
}
// 堆排序
void HeapSort(int* a, int n)
{
    assert(a);

    for (int i = (n - 2) / 2; i >= 0; i -- )
    {
        adjustdown(a, n, i);
    }

    int end = n - 1;
    while (end > 0)
    {
        swap(&a[end], &a[0]);
        adjustdown(a, end, 0);
        end -- ;
    }
}
// 冒泡排序
void BubbleSort(int* a, int n)
{
    assert(a);

    for (int i = 0; i < n - 1; i ++ )
    {
        bool flag = true;

        for (int j = 1; j < n - i; j ++ )
        {
            if (a[j - 1] > a[j])
            {
                swap(&a[j - 1], &a[j]);
                flag = false;
            }
        }

        if (flag) break;
    }
}

int mid_num(int* a, int l, int r)
{
    int mid = (l + r) / 2;
    if (a[l] < a[r])
    {
        if (a[l] > a[mid]) return l;
        else if (a[r] < a[mid]) return r;
        else return mid;
    }
    else 
    {
        if (a[r] > a[mid]) return r;
        else if (a[l] < a[mid]) return l;
        else return mid;
    }
}
// 快速排序
void QuickSort(int* a, int l, int r)
{
    if (l >= r) return;

    if (r - l + 1 > 10)
    {
        int mid = QuickSort22(a, l, r);
        QuickSort(a, l, mid - 1);
        QuickSort(a, mid + 1, r);
    }
    else 
    {
        InsertSort(a + l, r - l + 1);
    }
}
// 霍尔法
void QuickSort1(int* a, int l, int r)
{
    if (l >= r) return;

    int mid = mid_num(a, l, r);
    swap(&a[l], &a[mid]);
    int keyi = l, begin = l, end = r;

    while (begin < end)
    {
        while (begin < end && a[end] >= a[keyi]) end -- ;
        while (begin < end && a[begin] <= a[keyi]) begin ++ ;

        swap(&a[begin], &a[end]);
    }
    swap(&a[keyi], &a[begin]);

    QuickSort1(a, l, begin - 1);
    QuickSort1(a, begin + 1, r);
}
// 挖坑法
void QuickSort2(int* a, int l, int r)
{
    if (l >= r) return;

    int mid = mid_num(a, l, r);
    swap(&a[l], &a[mid]);
    int hole = l, key = a[l];
    int begin = l, end = r;

    while (begin < end)
    {
        while (begin < end && a[end] >= key) end -- ;
        a[hole] = a[end];
        hole = end;

        while (begin < end && a[begin] <= key) begin ++ ;
        a[hole] = a[begin];
        hole = begin;
    }
    a[hole] = key;

    QuickSort2(a, l, hole - 1);
    QuickSort2(a, hole + 1, r);
}
// 前后指针法
void QuickSort3(int* a, int l, int r)
{
    if (l >= r) return;

    int mid = mid_num(a, l, r);
    swap(&a[l], &a[mid]);
    int keyi = l, prev = l, cur = l + 1;

    while (cur <= r)
    {
        if (a[cur] < a[keyi] && ++ prev != cur)
            swap(&a[prev], &a[cur]);

        cur ++ ;
    }
    swap(&a[prev], &a[keyi]);

    QuickSort3(a, l, prev - 1);
    QuickSort3(a, prev + 1, r);
}
int QuickSort11(int* a, int l, int r)
{
    int mid = mid_num(a, l, r);
    swap(&a[l], &a[mid]);
    int keyi = l, begin = l, end = r;

    while (begin < end)
    {
        while (begin < end && a[end] >= a[keyi]) end -- ;
        while (begin < end && a[begin] <= a[keyi]) begin ++ ;

        swap(&a[begin], &a[end]);
    }
    swap(&a[keyi], &a[begin]);

    return begin;
}
int QuickSort22(int* a, int l, int r)
{
    int mid = mid_num(a, l, r);
    swap(&a[l], &a[mid]);
    int hole = l, key = a[l];
    int begin = l, end = r;

    while (begin < end)
    {
        while (begin < end && a[end] >= key) end -- ;
        a[hole] = a[end];
        hole = end;

        while (begin < end && a[begin] <= key) begin ++ ;
        a[hole] = a[begin];
        hole = begin;
    }
    a[hole] = key;

    return hole;
}
int QuickSort33(int* a, int l, int r)
{
    int mid = mid_num(a, l, r);
    swap(&a[l], &a[mid]);
    int keyi = l, prev = l, cur = l + 1;

    while (cur <= r)
    {
        if (a[cur] < a[keyi] && ++ prev != cur)
            swap(&a[prev], &a[cur]);

        cur ++ ;
    }
    swap(&a[prev], &a[keyi]);

    return prev;
}
// 快排非递归
void QuickSortNonR(int* a, int l, int r)
{
    assert(a);

    Stack st;
    StackInit(&st);

    STPush(&st, r);
    STPush(&st, l);

    while (!STEmpty(&st))
    {
        int begin = STTop(&st);
        STPop(&st);
        int end = STTop(&st);
        STPop(&st);

        int mid = QuickSort33(a, begin, end);

        if (mid + 1 < end)
        {
            STPush(&st, end);
            STPush(&st, mid + 1);
        }
        if (mid - 1 > begin)
        {
            STPush(&st, mid - 1);
            STPush(&st, begin);
        }
    }
}

// 归并排序
void MergeSort(int* a, int n)
{
    assert(a);

    int* tmp = (int*)malloc(sizeof(int) * n);
    assert(tmp);

    MergeSortY(a, 0, n - 1, tmp);

    free(tmp);
}
void MergeSortY(int* a, int l, int r, int* tmp)
{
    if (l >= r) return;

    int mid = (l + r) / 2;

    MergeSortY(a, l, mid, tmp);
    MergeSortY(a, mid + 1, r, tmp);

    int i = l, j = mid + 1;
    int k = l;

    while (i <= mid && j <= r)
    {
        if (a[i] < a[j])
        {
            tmp[k ++ ] = a[i ++ ];
        }
        else 
        {
            tmp[k ++ ] = a[j ++ ];
        }
    }
    while (i <= mid) tmp[k ++ ] = a[i ++ ];
    while (j <= r) tmp[k ++ ] = a[j ++ ];

    memcpy(a + l, tmp + l, sizeof(int) * (r - l + 1));
}
// 归并排序非递归
void MergeSortNonR(int* a, int n)
{
    assert(a);

    int* tmp = (int*)malloc(sizeof(int) * n);
    assert(tmp);

    MergeSortNonRY(a, 0, n - 1, tmp);

    free(tmp);
}
void MergeSortNonRY(int* a, int l, int r, int* tmp)
{
    int gap = 1;
    int n = r - l + 1;

    while (gap < n)
    {
        for (int i = 0; i < n; i += 2 * gap)
        {
            int begin1 = i, end1 = i + gap - 1;
            int begin2 = i + gap, end2 = i + 2 * gap - 1;

            if (begin2 >= n) break;
            if (end2 >= n) end2 = n - 1;

            int k = i;

            while(begin1 <= end1 && begin2 <= end2)
            {
                if (a[begin1] < a[begin2])
                {
                    tmp[k ++ ] = a[begin1 ++ ];
                }
                else 
                {
                    tmp[k ++ ] = a[begin2 ++ ];
                }
            }
            while (begin1 <= end1) tmp[k ++ ] = a[begin1 ++ ];
            while (begin2 <= end2) tmp[k ++ ] = a[begin2 ++ ];

            memcpy(a + i, tmp + i, sizeof(int) * (end2 - i + 1));
        }

        gap *= 2;
    }
}
// 计数排序
void CountSort(int* a, int n)
{
    assert(a);

    int max = a[0];
    int min = a[0];

    for (int i = 0; i < n; i ++ )
    {
        if (max < a[i]) max = a[i];
        if (min > a[i]) min = a[i];
    }

    int k = max - min + 1;

    int* countA = (int*)malloc(sizeof(int) * k);
    assert(countA);

    for (int i = 0; i < n; i ++ )
    {
        countA[a[i] - min] ++ ;
    }
    
    int j = 0;
    for (int i = 0; i < k; i ++ )
    {
        while (countA[i] -- )
        {
            a[j ++ ] = i + min;
        }
    }

    free(countA);
}
